package com.iot08.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iot08.api.service.DeviceService;
import com.iot08.api.service.OpenInterfaceService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 5/25/22 2:01 PM
 */
@Slf4j
@RestController
@RequestMapping("getway")
public class OpenInterfaceController {

    @Resource
    private OpenInterfaceService openInterfaceService;


    //对外开放的接口 根据IMEI号获取最近一次定位信息的接口
    @GetMapping("/open-interface/last-loction")
    public ResponseMap getLastLoction(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.getLastLoction(paramsMap, request);
        return res;
    }

    //根据IMEI号下发一次定位信息 (间隔30s)
    @PostMapping("/open-interface/cmd-devloc")
    public ResponseMap postCmdDevloc(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.postCmdDevloc(paramsMap, request);
        return res;
    }

    //对外开放的接口 根据IMEI号获取指定时间段位置信息的接口
    @GetMapping("/open-interface/history")
    public ResponseMap getHistory(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.getHistory(paramsMap, request);
        return res;
    }

    //对外开放的接口 根据IMEI号 获取/添加/修改/删除 电子围栏接口
    @PostMapping("/open-interface/operation-devrail")
    public ResponseMap operationDevrail(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.operationDevrail(paramsMap, request);
        return res;
    }


    //获取 设备上传的定位间隔时间
    @GetMapping("/open-interface/devlocatetime")
    public ResponseMap getDevlocatetime(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.getDevlocatetime(paramsMap, request);
        return res;
    }

    //设置 设备上传的定位间隔时间
    @PostMapping("/open-interface/devlocatetime")
    public ResponseMap postDevlocatetime(HttpServletRequest request) {
        JSONObject paramsMap = getParameter(request);
        ResponseMap res = openInterfaceService.postDevlocatetime(paramsMap, request);
        return res;
    }

    private JSONObject getParameter( HttpServletRequest request) {
        Enumeration<String> parameterNames = request.getParameterNames();
        JSONObject jsonObject = new JSONObject();
        if(parameterNames.hasMoreElements()){
            // 获取URL的传参
            while (parameterNames.hasMoreElements()){
                String key = parameterNames.nextElement();
                jsonObject.put(key, request.getParameter(key));
            }
        }else{
            // 获取body的传参
            try {
                String reader = getAllRequestParam2(request);
                if(reader.length()>1){
                    if(reader.substring(0,1).equals("[")){

                    }else{
                        JSONObject tempOB = JSONObject.parseObject(reader);
                        jsonObject.putAll(tempOB);
                    }
                }

            } catch (Exception e) {
                log.error("获取参数错误"+e.getMessage(),e);
            }
        }

        return jsonObject;
    }

    public static String getAllRequestParam2(HttpServletRequest request) throws IOException {
        BufferedReader br = request.getReader();
        String str;
        StringBuilder wholeStr = new StringBuilder();
        while((str = br.readLine()) != null){
            wholeStr.append(str);
        }
        return wholeStr.toString();
    }




}
